package com.simon.hse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.R;
import com.simon.hse.activity.law.LawDetailActivity;
import com.simon.hse.activity.law.LawManageActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 消息
 */
@SuppressWarnings("all")
public class MessageFragment extends BaseFragment {

    @ViewInject(R.id.smartrefresh)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.recyclerview)
    RecyclerView mRecyclerView;

    @ViewInject(R.id.iv_message_delete)
    ImageView mIv_message_delete;

    @ViewInject(R.id.ll_message_more)
    LinearLayout mLl_message_more;

    private CommonEmptyAdapter mCommonEmptyAdapter;
    private int currentPage = 1, totalPage = 1;
    private List<String> mDataBeanList = new ArrayList<>();// 法律法规列表
    private Map<Integer, Boolean> mSelectedMap = new HashMap<>();
    private boolean isShowMore = false;// 是否展示更多筛选框

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_message_delete, R.id.ll_msg_more, R.id.tv_message_more_reset, R.id.tv_message_more_confirm})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_message_delete:
                boolean delState = false;
                Iterator<Map.Entry<Integer, Boolean>> iterator = mSelectedMap.entrySet().iterator();
                StringBuffer sb = new StringBuffer();
                while (iterator.hasNext()) {
                    Map.Entry<Integer, Boolean> entry = iterator.next();
                    Boolean value = entry.getValue();
                    if (value) {
                        delState = true;
                        sb.append(entry.getKey() + "、");
                    }
                }
                if (delState) {
                    ToastUtils.getInstance().showShortToast("即将删除：" + sb.toString());
                    // 放到网络请求结果
                    mSelectedMap.clear();
                    mCommonEmptyAdapter.notifyDataSetChanged();
                } else {
                    mCommonEmptyAdapter.notifyDataSetChanged();
                }
                mIv_message_delete.setVisibility(View.INVISIBLE);
                break;
            case R.id.ll_msg_more:
                if (isShowMore) {
                    mLl_message_more.setVisibility(View.GONE);
                } else {
                    mLl_message_more.setVisibility(View.VISIBLE);
                }
                isShowMore = !isShowMore;
                break;
            case R.id.tv_message_more_reset:
                mLl_message_more.setVisibility(View.GONE);
                isShowMore = !isShowMore;
                break;
            case R.id.tv_message_more_confirm:
                mLl_message_more.setVisibility(View.GONE);
                isShowMore = !isShowMore;
                break;
        }
    }

    private void initRecyclerView() {
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(getContext(), R.layout.layout_item_msg, mDataBeanList);
        mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<String>() {
            @Override
            public void setItemDatas(CommonViewHolder holder, String dataBean, int position) {
                CheckBox cb_message = holder.getView(R.id.cb_message);
                Boolean aBoolean = mSelectedMap.get(position);
                if (aBoolean != null && aBoolean) {
                    cb_message.setVisibility(View.VISIBLE);
                    cb_message.setChecked(true);
                } else {
                    cb_message.setVisibility(View.INVISIBLE);
                    cb_message.setChecked(false);
                }

                cb_message.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("Simon", "setOnClickListener = " + position + " - " + !mSelectedMap.get(position));
                        mSelectedMap.put(position, !mSelectedMap.get(position));
                    }
                });

            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {
                initData();
            }

            @Override
            public void setOnItemClickListener(View view, int position) {
                boolean delState = false;
                Iterator<Map.Entry<Integer, Boolean>> iterator = mSelectedMap.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<Integer, Boolean> entry = iterator.next();
                    Boolean value = entry.getValue();
                    if (value) {
                        delState = true;
                    }
                }
                if (!delState) {
                    if (!isShowMore) {
                        ToastUtils.getInstance().showShortToast("跳转消息详情页面");
                    }
                }
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {
                Log.i("Simon", "setOnItemLongClickListener = " + position);
                mSelectedMap.put(position, true);
                mIv_message_delete.setVisibility(View.VISIBLE);
                mCommonEmptyAdapter.notifyItemChanged(position);
            }
        });

        mCommonEmptyAdapter.setHasStableIds(true);
        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    /**
     *
     */
    private void initData() {
        for (int i = 0; i < 10; i++) {
            mSelectedMap.put(i, false);
            mDataBeanList.add(i + "");
        }
        mCommonEmptyAdapter.notifyDataSetChanged();

        mSmartRefresh.finishRefresh();
        mSmartRefresh.finishLoadMore();
    }

}
